<?php
/**
 * @author vladone
 * @copyright 2007
 */
include ('include/biff.php');
include ('include/conf.php');
function Export($operatie, $result)
{
    $myxls = new BiffWriter();
    // file name suggested to the browser (stream)
    $myxls->outfile = date("d-m-Y") . ".xls";

    // create 4 default fonts
    $myxls->xlsSetFont('Arial', 10, FONT_NORMAL);
    $myxls->xlsSetFont('Arial', 10, FONT_BOLD);
    $myxls->xlsSetFont('Courier New', 8, FONT_NORMAL);
    $myxls->xlsSetFont('Courier New', 8, FONT_BOLD);

    // set default row height to 14 points
    $myxls->xlsSetDefRowHeight(14);
    $myxls->xlsSetColWidth(1, 1, 20);
    $myxls->xlsSetColWidth(3, 3, 10);
    // turn  backup flag on (xlk)
    $myxls->xlsSetBackup();

    // print grid lines
    $myxls->xlsSetPrintGridLines();

    // print row (1,2...) and col (A, B..) references
    $myxls->xlsSetPrintHeaders();

    //print header
    $fields = mysql_num_fields($result);
    //$myxls->xlsSetColWidth(0,$fields,100);
    for ($i = 0; $i < $fields; $i++)
    {
        $myxls->xlsWriteText(0, $i, mysql_field_name($result, $i), 0, 0, FONT_0,
            ALIGN_CENTER);
    }
    //print data
    $linie = 1;
    while ($row = mysql_fetch_row($result))
    {
        // $col contorizeaza coloana pentru a gasi si formata data
        $coloana = 0;
        foreach ($row as $value)
        {
            if ((!isset($value)) or ($value == ""))
            {
                $myxls->xlsWriteText($linie, $coloana, "", 0, 0, FONT_0, ALIGN_CENTER);
            }
            else
            {
                if ($coloana == 5)
                {
                    $value = date("d-m-Y H:i", $value);
                }
                //$value = str_replace('"', '""', $value);
                //$value = '"' . $value . '"';
                $myxls->xlsWriteText($linie, $coloana, $value, 0, 0, FONT_0, ALIGN_CENTER);
            }
            $coloana += 1;
        }
        $linie += 1;
    }
    // assemble the stream
    // and either send the stream to the browser or
    // save it as a file in the location/filename passed via $fname
    $fname = date("d-m-Y", time()) . "_" . $operatie . "_".random_str(2).".xls";
    $myxls->xlsParse($fname);
    //fisierul a fisierul a fost salvat pe hard. Se verifica daca exista si se intoarce mesaj catre client
    if (file_exists($fname))
    {
        echo "&file=$fname&flag=1&";
    }
    else
    {
        echo "&file=Error saving file!&flag=0&";
    }
    return;
}
$id_connect = connect_to_database(HOST, USER, PASS, 'server_imei2');
//se verifica autentificarea
$sesiune = mysql_real_escape_string($_GET['sesiune']);
$str = "SELECT tip_cont FROM clienti WHERE sesiune = '$sesiune'";
$result = mysql_query($str, $id_connect);
$num_rows = mysql_num_rows($result);
if ($num_rows == 1)
{
    $value = mysql_fetch_assoc($result);
    $tip_cont = $value['tip_cont'];
    if ($tip_cont != 'admin')
    {
        exit();
    }
}
else
{
    exit();
}
unset($str);
//begin selection
$operatie = $_GET['op'];
if ($operatie == 'delete')
{
    $filename = $_SERVER['DOCUMENT_ROOT'] . "/" . $_GET['filename'];
    if (file_exists($filename))
    {
        unlink($filename);
        echo "&flag=1&";
    }
    else
    {
        echo "&flag=0&";
    }
}
if ($operatie == 'get_imei')
{
    $select = "SELECT nume,imei,unlock_code,model,companie,data_in,cost FROM coduri ";
    $where = "WHERE ";
    $where_array = array();
    $imei_status = $_GET['imei_status'];
    $nume = $_GET['user'];
    $model = $_GET['model'];
    $companie = $_GET['companie'];
    if ($imei_status == "wait")
    {
        array_push($where_array, "status='wait'");
    }
    if ($imei_status == "done")
    {
        array_push($where_array, "status='done'");
    }
    if ($nume != "all")
    {
        array_push($where_array, "nume='$nume'");
    }
    if ($model != "all")
    {
        array_push($where_array, "model='$model'");
    }
    if ($companie != "all")
    {
        array_push($where_array, "companie='$companie'");
    }
    for ($i = 0; $i < count($where_array); $i++)
    {
        $where .= $where_array[$i];
        if ($i < count($where_array) - 1)
        {
            $where .= " AND ";
        }
    }
    $str = $select . $where . " ORDER BY model ASC;";
}
if ($operatie == 'get_mail')
{
    $str = "SELECT mail FROM clienti order by mail ASC";
}
if (isset($str))
{
    $result = mysql_query($str, $id_connect);
    Export($operatie, $result);
}
?>